package m1.server.composant.database;

import m2.Composant;
import m2.Configuration;

public class DataBase extends Composant {
	/**
	 * Permet de savoir dans quel �tat se trouve la Database
	 */
	int etat;
	/**
	 * Service requis
	 */
	private ReceiveRequestIntS receiveRequestInfS;
	/**
	 * Service requis
	 */
	private ReceiveRequestSecurityS receiveRequestSecurityS;
	/**
	 * Service fourni
	 */
	private SendAnswerSecurityS sendAnswerSecurityS;
	/**
	 * Service fourni
	 */
	private SendRequestIntS sendrequestInfS;

	public DataBase(Configuration parent) {
		super("DataBase", parent);
		etat = 0;

		receiveRequestInfS = new ReceiveRequestIntS();
		receiveRequestInfS.setComposant(this);
		addServiceRequis(receiveRequestInfS);
		addPortRequis(receiveRequestInfS.getPortRequis());

		receiveRequestSecurityS = new ReceiveRequestSecurityS();
		receiveRequestSecurityS.setComposant(this);
		addServiceRequis(receiveRequestSecurityS);
		addPortRequis(receiveRequestSecurityS.getPortRequis());

		sendAnswerSecurityS = new SendAnswerSecurityS();
		sendAnswerSecurityS.setComposant(this);
		addServiceFourni(sendAnswerSecurityS);
		addPortFournis(sendAnswerSecurityS.getPortFourni());

		sendrequestInfS = new SendRequestIntS();
		sendrequestInfS.setComposant(this);
		addServiceFourni(sendrequestInfS);
		addPortFournis(sendrequestInfS.getPortFourni());
	}

	@Override
	public void update(Object o, Object arg) {
		/**
		 * Ici � lieu la validation ou non de l'identifiant Ici � lieu l'envoie
		 * du r�sultat de la requ�te
		 */
		switch (etat) {
		case 0:
			etat++;
			String identifiant = (String) arg;
			// identifiant = identifiant.split("#")[0];
			sendAnswerSecurityS.activer("Ok : " + identifiant.split("#")[0]
					+ " peut effectuer la requete suivante # "
					+ identifiant.split("#")[1]);
			break;
		case 1:
			etat++;
			sendrequestInfS.activer("Data correspondant a " + ((String) arg).split("#")[1]);
		default:
			;
		}
	}
}